{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Encode and Decode TinyURL"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #design #hash-table #string #hash-function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #设计 #哈希表 #字符串 #哈希函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: CodecDriver"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #TinyURL 的加密与解密"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>TinyURL 是一种 URL 简化服务， 比如：当你输入一个 URL&nbsp;<code>https://leetcode.com/problems/design-tinyurl</code>&nbsp;时，它将返回一个简化的URL&nbsp;<code>http://tinyurl.com/4e9iAk</code> 。请你设计一个类来加密与解密 TinyURL 。</p>\n",
    "\n",
    "<p>加密和解密算法如何设计和运作是没有限制的，你只需要保证一个 URL 可以被加密成一个 TinyURL ，并且这个 TinyURL 可以用解密方法恢复成原本的 URL 。</p>\n",
    "\n",
    "<p>实现 <code>Solution</code> 类：</p>\n",
    "\n",
    "<div class=\"original__bRMd\">\n",
    "<div>\n",
    "<ul>\n",
    "\t<li><code>Solution()</code> 初始化 TinyURL 系统对象。</li>\n",
    "\t<li><code>String encode(String longUrl)</code> 返回 <code>longUrl</code> 对应的 TinyURL 。</li>\n",
    "\t<li><code>String decode(String shortUrl)</code> 返回 <code>shortUrl</code> 原本的 URL 。题目数据保证给定的 <code>shortUrl</code> 是由同一个系统对象加密的。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>url = \"https://leetcode.com/problems/design-tinyurl\"\n",
    "<strong>输出：</strong>\"https://leetcode.com/problems/design-tinyurl\"\n",
    "\n",
    "<strong>解释：</strong>\n",
    "Solution obj = new Solution();\n",
    "string tiny = obj.encode(url); // 返回加密后得到的 TinyURL 。\n",
    "string ans = obj.decode(tiny); // 返回解密后得到的原本的 URL 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= url.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li>题目数据保证 <code>url</code> 是一个有效的 URL</li>\n",
    "</ul>\n",
    "</div>\n",
    "</div>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [encode-and-decode-tinyurl](https://leetcode.cn/problems/encode-and-decode-tinyurl/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [encode-and-decode-tinyurl](https://leetcode.cn/problems/encode-and-decode-tinyurl/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"https://leetcode.com/problems/design-tinyurl\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# ### 官1：非ascii字符来识别字符为空的情况(unichr函数，python3废弃--无法调试成功)\n",
    "# class Codec:\n",
    "#     def encode(self, strs):\n",
    "#         \"\"\"Encodes a list of strings to a single string.\n",
    "#         :type strs: List[str]\n",
    "#         :rtype: str\n",
    "#         \"\"\"\n",
    "#         if len(strs) == 0: \n",
    "#             # return unichr(258) ### python-2 有unichr函数\n",
    "#             return chr(258)\n",
    "        \n",
    "#         # encode here is a workaround to fix BE CodecDriver error\n",
    "#         # return unichr(257).join(x.encode('utf-8') for x in strs)\n",
    "#         return chr(257).join(x.encode('utf-8') for x in strs)\n",
    "        \n",
    "\n",
    "#     def decode(self, s):\n",
    "#         \"\"\"Decodes a single string to a list of strings.\n",
    "#         :type s: str\n",
    "#         :rtype: List[str]\n",
    "#         \"\"\"\n",
    "#         # if s == unichr(258): \n",
    "#         #     return []\n",
    "#         # return s.split(unichr(257))\n",
    "\n",
    "#         if s==chr(258):return []\n",
    "#         return s.split(chr(257))\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))\n",
    "\n",
    "# 官1：网友py3版\n",
    "class Codec:\n",
    "    def encode(self,strs):\n",
    "        if len(strs)==0:\n",
    "            return chr(258)\n",
    "\n",
    "        return chr(257).join(strs)\n",
    "\n",
    "    def decode(self,s):\n",
    "        if s==chr(258): return []\n",
    "        return s.split(chr(257))\n",
    "\n",
    "# 官2：基于http1.1使用的编码，不依赖与输入字符集\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs):\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        :type strs: List[str]\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        if len(strs) == 0: \n",
    "            return chr(258)\n",
    "        \n",
    "        # encode here is a workaround to fix BE CodecDriver error\n",
    "        return chr(257).join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s):\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        :type s: str\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        if s == chr(258): \n",
    "            return []\n",
    "        return s.split(chr(257))\n",
    "\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
